package com.homework;

import java.util.ArrayList;
import java.util.Objects;

public class homework5 {
    /*
分析以下需求，并用代码实现
		1.定义Student类
			属性:
				姓名：String name
				年龄：int age
				成绩：int score
			行为:
				空参构造方法
				有参构造方法
				set和get方法
				toString方法
		2.定义测试类,进行测试
			(1)创建10个学生对象存入ArrayList集合中
			(2)打印最高分的学员姓名、年龄、成绩  [要求封装1个方法  参数是集合对象   返回值类型为Student]
			(3)打印10个学生的总成绩和平均分		 [要求封装两个方法完成]
			(4)打印不及格的学员信息及数量		 [要求封装一个方法完成]     */
    public static void main(String[] args) {
        ArrayList<Student3> student3ArrayList = new ArrayList<>();
        Student3[] student3s = new Student3[10];
        student3s[0] = new Student3("张三",18,65);
        student3s[1] = new Student3("李四",19,87);
        student3s[2] = new Student3("王五",20,85);
        student3s[3] = new Student3("赵六",20,98);
        student3s[4] = new Student3("胡七",18,93);
        student3s[5] = new Student3("曹八",21,59);
        student3s[6] = new Student3("刘九",18,88);
        student3s[7] = new Student3("陆十",19,98);
        student3s[8] = new Student3("赢一",20,60);
        student3s[9] = new Student3("唐二",22,78);
        for (int i = 0; i < 10; i++) {
            student3ArrayList.add(student3s[i]);
        }
        Student3 maxStudent = getMaxStudent(student3ArrayList);
        System.out.println("maxStudent = " + maxStudent);
        int averageScore = getAverageScore(student3ArrayList);
        System.out.println("averageScore = " + averageScore);
        getCount(student3ArrayList);
    }
    public static Student3 getMaxStudent(ArrayList<Student3> student3ArrayList){
        int max = student3ArrayList.get(0).getScore();
        for (Student3 student3 : student3ArrayList) {
            if (student3.getScore() > max){
                max = student3.getScore();
            }
        }
        Student3 index = student3ArrayList.get(0);
        for (int i = 0; i < student3ArrayList.size(); i++) {
            if (student3ArrayList.get(i).getScore() == max){
                index = student3ArrayList.get(i);
            }
        }
        return index;
    }
    public static int getAverageScore(ArrayList<Student3> student3ArrayList){
        int sum = 0;
        for (Student3 student3 : student3ArrayList) {
            sum += student3.getScore();
        }
        return sum/student3ArrayList.size();
    }
    public static void getCount(ArrayList<Student3> student3ArrayList){
        int count = 0;
        for (Student3 student3 : student3ArrayList) {
            if (student3.getScore() < 60){
                System.out.println(student3);
                count++;
            }
        }
        System.out.println("不及格数量为:" + count);
    }
}
class Student3{
    private String name;
    private Integer age;
    private Integer score;

    public Student3() {
    }

    public Student3(String name, Integer age, Integer score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student3{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student3 student3 = (Student3) o;
        return Objects.equals(name, student3.name) && Objects.equals(age, student3.age) && Objects.equals(score, student3.score);
    }

    @Override
    public int hashCode() {
        int result = Objects.hashCode(name);
        result = 31 * result + Objects.hashCode(age);
        result = 31 * result + Objects.hashCode(score);
        return result;
    }
}